<template>
  <el-dialog
    title="新增员工"
    :visible="dialogFormVisible"
    class="form"
    @close="cancelBtn"
  >
    <el-row align="center">
      <el-form
        :model="form"
        label-width="120px"
        label-position="right"
        :rules="rules"
        ref="addForm"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择"
            style="width: 80%"
           
          >
            <el-option
              label="非正式"
              :value="2"
            ></el-option>
            <el-option
              label="正式"
              :value="1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="form.departmentName"
            style="width: 80%"
            @focus="getDepart"
            placeholder="请选择部门"
          ></el-input>
          <el-tree
            v-if="showTree"
            v-loading="loading"
            :data="depart"
            default-expand-all=""
            :props="{ label: 'name' }"
            @node-click="selectNode"
          />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="cancelBtn">取消</el-button>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>

<script>
import {addEmployee} from "@/api/employees"
import { getDepartments } from "@/api/departments";
export default {
  name: "addEmployee",
  data() {
    return {
      showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      depart: [],
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      rules: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
    };
  },
  props: ["dialogFormVisible"],
  methods: {
    cancelBtn() {
      this.$emit("update:dialogFormVisible", false);
      this.form = {},
      this.$refs.addForm.resetFields();
    },
    // 点击确定
    btnOk() {
      this.$refs.addForm.validate(async (valid)=>{
        if(valid){
          try {
            console.log("ok");
          let result = await addEmployee(this.form);
          console.log(result);
          this.$message({
            type: "success",
            message: "新增成功！",
          });
          } catch (error) {
            this.$message({
            type: "info",
            message: "新增失败！",
          });
          }
        }else{
          console.log("错误提交！");
        }
        this.$emit("update:dialogFormVisible", false);
        this.$emit("getRole");
      })
    },
    async getDepart() {
      this.showTree = true;
      this.loading = true;
      let result = await getDepartments();
      this.depart = this.formatDeparts(result.depts, "");
      console.log(this.depart);
      this.loading = false;
    },
    formatDeparts(depart, rootValue) {
      let arr = [];
      depart.forEach((item) => {
        if (item.pid === rootValue) {
          // 找到之后 就要去找 item 下面有没有子节点
          const children = this.formatDeparts(depart, item.id);
          if (children.length) {
            // 如果children的长度大于0 说明找到了子节点
            item.children = children;
          }
          arr.push(item); // 将内容加入到数组中
        }
      });
      return arr;
    },
    selectNode(node) {
      console.log(node);
      this.form.departmentName = node.name;
      this.showTree = false;
    },
  },
};
</script>

<style>
</style>